var sinoUtil = {

    //请求网关前缀

    httpPrefix: '',
    httpNginxPrefix: '',
    // 字段的验证，支持非空、手机、邮箱的判断
    validate: function (value, type) {
        var value = $.trim(value);
        // 非空验证
        if ('require' === type) {
            //将value强转为boolean类型的值
            return !!$.trim(value);
        }
        // 数字验证
        if ('number' === type) {
            return /^[0-9]*$/.test(value);
        }
        // 带小数的数字验证
        if ('decimals' === type) {
            return /^[0-9]+\.?[0-9]*$/.test(value) || /^-[0-9]+\.?[0-9]*$/.test(value);
        }
        // 特殊字符验证
        if ('specialCharacter' === type) {
            return !/[~'!@#&￥$%^*()+-/\\_=:;,.{}！（）——：；，。【】]/.test(value);
        }
        // 特殊字符验证(富文本)
        if ('specialCharacterUeditor' === type) {
            var arr = ["\\b", "\\f", "\\n", "\\r", "\\t", "\\\\", "\\"];
            var flag = true;
            for (var i = 0; i < arr.length; i++) {
                if (value.indexOf(arr[i]) != -1) {
                    flag = false;
                    break;
                }
            }
            return flag;
        }
        // 特殊字符验证(产品)
        if ('specialCharacterProduct' === type) {
            return !/[_,.{}——，。【】]/.test(value);
        }
        // 特殊字符验证(类目管理)
        if ('specialCharacterCatalog' === type) {
            return !/[~'!@#￥$%^*()+_=:\\,.{}！（）——：，。【】]/.test(value);
        }
        // 手机号验证
        if ('phone' === type) {
            return /^1\d{10}$/.test(value);
        }
        // 邮箱验证
        if ('email' === type) {
            // return /^(\w)+(\.\w+)*@(\w)+((\.\w{2,3}){1,3})$/.test(value);
            return /^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/.test(value);
        }
        // 密码验证
        if ('password' === type) {
            return /(?!^\\d+$)(?!^[a-zA-Z]+$)(?!^[_#@]+$).{6,}/.test(value);
        }
        // 域名校验 只要有http:// 或者 https://开头就可以
        if ('link' === type) {
            return /^((http:\/\/)|(https:\/\/)){1}[\s\S]*$/.test(value)
        }
        if ('english' === type) {
            return /^[a-zA-Z]{1,10}$/.test(value);
        }
        if ('float-point' === type) {
            return /^(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*))$/.test(value);
        }
        if ('twosize' === type) {
            var array = value.split(",");
            if (array.length != 2) {
                return false;
            } else {
                return true;
            }
        }
    },
    // 字段的验证失败时提示样式
    validateAndChangeClass: function (target, flag, message) {
        if (!flag) {
            $(target).addClass("error");
            $(target).next("p").remove();
            $(target).after("<p class='error'>" + message + "</p>");
            return false;
        } else {
            $(target).removeClass("error");
            $(target).next("p").remove();
            return true;
        }
    },
    // 请求发送封装
    sendHttp: function (url, type, param, success, error, isAsync) {
        $.ajax({
            type: type,
            url: url,
            data: param,
            async: !isAsync ? false : true,
            dataType: 'json',
            success: success,
            error: error,
            beforeSend: function (XMLHttpRequest) {
                //HttpUtility.UrlEncode
                XMLHttpRequest.setRequestHeader("Authorization", window.localStorage.token ? window.localStorage.token : null);
            }
        });
    },
    //加载loading
    loading: function () {
        return layer.load(0)
    },
    // 取消loading并显示结果 state 1 正确  2 错误
    stopLoadingAndShowResult: function (index, state, msg, time) {
        layer.close(index)
        if (msg) {
            layer.msg(msg, {
                icon: state,
                time: time ? time : 2000 //2秒关闭（如果不配置，默认是3秒）
            })
        }
    },
    dateToString: function (time) {
        if(time==''||time==null){
            return "";
        }
        var datetime = new Date();
        datetime.setTime(time);
        var year = datetime.getFullYear();
        var month = datetime.getMonth() + 1;
        var date = datetime.getDate();
        var hour = datetime.getHours();
        /*对月 日 时 分 秒 小于10的时候的处理  --小于 10 时前面加 0*/
        if (month <= 9) {
            month = "0" + month;
        }
        if (date <= 9) {
            date = "0" + date
        }
        if (hour <= 9) {
            hour = "0" + hour;
        }
        var minute = datetime.getMinutes();
        if (minute <= 9) {
            minute = "0" + minute;
        }
        var second = datetime.getSeconds();
        if (second <= 9) {
            second = "0" + second;
        }
        return year + "-" + month + "-" + date + " " + hour + ":" + minute
            + ":" + second;
    },
    getStaticData : function (target, groupname) {  //通过groupname获取字典表中的值并赋给target下拉框
        sinoUtil.sendHttp(sinoUtil.httpPrefix + "/api-system/getStaticData", "POST", {groupname : groupname}, function(res){
            var data = res.data
            var option = "<option value=''>请选择</option>";
            for(var i = 0 ; i < data.length ; i++){
                option += "<option value='"+ data[i].value +"'>"+ data[i].name +"</option>";
            }
            $("#" + target).empty();
            $("#" + target).append(option);
        }, function(error){}, false);
    },
    getStaticData : function (target, groupname, useId) {  //通过groupname获取字典表中的值并赋给target下拉框
        sinoUtil.sendHttp(sinoUtil.httpPrefix + "/api-system/getStaticData", "POST", {groupname : groupname}, function(res){
            var data = res.data
            var option = "<option value=''>请选择</option>";
            for(var i = 0 ; i < data.length ; i++){
                if (useId) {
                    option += "<option value='"+ data[i].id +"'>"+ data[i].name +"</option>";
                } else {
                    option += "<option value='"+ data[i].value +"'>"+ data[i].name +"</option>";
                }
            }
            $("#" + target).empty();
            $("#" + target).append(option);
        }, function(error){}, false);
    }
}